<?php

namespace App\Console\Commands;

use App\Model\Branch;
use App\Model\Order;
use App\Model\PushOrder;
use App\Model\tel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class qqPush extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'qqPush';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        while(true){
            try {
                //控制是否推单给下线
                $branch = Branch::where("id", 22)->first();
//            $branch = BranchTest::where("id",17)->first();
                if ($branch['tel'] == 1) {
                    $order = DB::select("select id,buytime,chargeaccount,price,num,orderno,`order`,callbackUrl from tiktok_tel where status = '未处理'   order by id asc limit 1");
//                $order = DB::select("select id,buytime,chargeaccount,remainingnumber,price,num,orderno from tiktok_order1 where status = '未处理'   order by id asc limit 1");
                    $num = Branch::where("tel", 1)->count();
                    if (isset($order[0])) {
                        $order1 = $order[0];
                        $order = (array)$order1;
                        if ($order['id'] % 1 == 0) {

//                        if ($order['id'] % 1 == 0) {
                            $res = $this->hfPush($order['orderno'], $order['chargeaccount'], $order['price']);

                            $info['ext'] = $res;
                            tel::where("orderno", $order['orderno'])->update($info);
//                            Order1::where("orderno",$order['orderno'])->update($info);
                            $res=simplexml_load_string($res);

                            if ($res->code == "00") {
                                $info['status'] = '已处理';
                                $info['branch'] = '已推送,等待反馈';
                                $info['isok'] = 3;
                                $info['pid'] = 22;
                                $info['time'] = time();
                                tel::where('orderno', $order['orderno'])->update($info);
                            }else{
                                $info['status'] = '已处理';
                                $info['branch'] = '充值失败';
                                $info['isok'] = 2;
                                $info['pid'] = 22;
                                $info['time'] = time();
                                tel::where('orderno', $order['orderno'])->update($info);
                                $data = [
                                    "order" =>  $order['order'],
                                    "orderno" =>  $order['orderno'],
                                    "chargeaccount" =>  $order['chargeaccount'],
                                    "order_status" =>  2,
                                    "finish_time" =>  time(),
                                ];
                                curlPost($order['callbackUrl'],json_encode($data));
                            }
                        }
                    }
                }

                //更新下游的平均完成时间
                echo isset($order['id']) ? $order['id'] : "";
            } catch (\Exception $e) {
                echo $e->getMessage();
            }
            sleep(5);
        }
    }

    public function hfPush($customer_order_no, $charge_account, $money)
    {
        try {
            $url = "http://api.xycz.shqunqiu.com:8160/unicomAync/buy.do";
            $callBackUrl = 'http://112.124.24.221/admin/callback/qq_AutoConfirmOrder';
            $privatekey = "da947f8f5052d22c91f460d7aab238bb9bf47629dcecf52fba1230b56b50ab95";
//            $privatekey = "ff25559b37c11cad6ef3927b716e2326a474953913ccb7b52c7eb9a0217af217";

            switch ($money){
//                case 30:
//                    $itemId = 20113;
//                    break;
                case 50:
                    $itemId = 20244;
                    break;
                case 100:
                    $itemId = 20245;
                    break;
                case 200:
                    $itemId = 20246;
                    break;
                case 300:
                    $itemId = 20247;
                    break;
                case 500:
                    $itemId = 20248;
                    break;
            }

            $data = [
                "userId" => 1080,
                "itemId" => $itemId,
                "uid" => $charge_account,
                "serialno" => $customer_order_no,
                "checkItemFacePrice" => $money*1000,
                "dtCreate" => date("YmdHis",time())
            ];
            $data1 = [
                "userId" => '1758',
                "itemId" => '216749',
                "uid" => '18958141603',
                "serialno" => '1575532779483',
                "checkItemFacePrice" => 30000,
                "dtCreate" => '20191205155939'
            ];
            ksort($data);

            $str = "";
            $str1 = "";
            foreach ($data as $v){
                $str .= $v;
            }
            $str .= $privatekey;
            $data['sign'] = md5($str);
            ksort($data);

            foreach ($data as $k=>$v){
                $str1 .= $k.'='.$v."&";
            }

            $str1 = substr($str1,0,-1);

            $res = $this->curlPost($url, $str1);

            return $res;
        }catch (\Exception $e){
            echo $e->getLine();
        }
    }

    public function curlPost($url, $post_data = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'charset=utf-8'));
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}
